<template>
    <div class="selectProducts">
        <div class="selectProducts_back">
            <div class="bg-white-height"></div>
            <div class="van-nav-bar">
                <div class="title">已选品</div>
                <van-icon name="arrow-left" class="left_img" @click="handleLeft" />
            </div>
            <div class="switch">
                <div class="switch_div">
                    <div :class="switchIndex == index ? 'switch_div_item_active' : 'switch_div_item'"
                        @click="switchClick(index)" v-for="(item, index) in switchList" :key="index">{{ item }}</div>
                </div>
            </div>

        </div>
        <div class="approval_screen">
            <div class="approval_screen_category" @click="categoryShow = true">
                {{ categoryName }}
                <van-image :src="dropDown"></van-image>
            </div>
            <div class="home-index-data-date">
                <div class="home-index-data-date-first">
                    <div class="home-index-data-date-first-date" @click="showPicker = false">
                        {{ currentDateFormat }}
                    </div>
                    <!-- <van-image width="10" height="6" :src="lsxsjImage" /> -->
                </div>
                <van-popup v-model:show="showPicker" position="bottom">
                    <van-date-picker @confirm="onConfirm" @cancel="showPicker = false" v-model="currentDate"
                        :max-date="maxDate" />
                </van-popup>
                <div class="home-index-data-date-middle">至</div>
                <div class="home-index-data-date-second">
                    <div class="home-index-data-date-second-date" @click="showPickerBottom = false">
                        {{ currentDateFormatInterval }}
                    </div>
                    <!-- <van-image width="10" height="6" :src="lsxsjImage" /> -->
                </div>
                <van-popup v-model:show="showPickerBottom" position="bottom">
                    <van-date-picker @confirm="onConfirmBottom" @cancel="showPickerBottom = false"
                        v-model="currentDateBottom" :min-date="minDate" />
                </van-popup>
            </div>
            <van-action-sheet title="选品部门" v-model:show="categoryShow" :actions="categoryList"
                @select="onSelectCategory" />
        </div>
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
                <template v-slot:finished>
                    <div class="nothing_div" v-if="list.length == 0">
                        <van-image :src="nothingData"></van-image>
                        <div class="nothing_font">暂无数据</div>
                    </div>
                </template>
                <div class="product_list_van_list">
                    <div v-for="(item, index) in list" :key="index" class="product_list_van_list_div"
                        @click="handleGetDetails(item.selectionId, item)">
                        <div class="titles">{{ item.coreName }}{{ item.selectionTime.split(' ')[0].split('-')[1] }}月{{
                            item.selectionTime.split(' ')[0].split('-')[2]
                            }}日选品</div>
                        <div class="center flex">
                            <div class="title">选品总数</div>
                            <div class="number">{{ item.selectionNum }}</div>
                        </div>
                        <div class="bottom flex">
                            <div class="grey"></div>
                            <div class="grey">{{ item.selectionTime }}</div>
                        </div>
                    </div>
                </div>
            </van-list>
        </van-pull-refresh>
    </div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { queryOperationDeptList } from '@/api/productList/index'
import { getSelectedProduct } from '@/api/approval/index'
const dropDown = new URL("@/assets/approval/drop_down .png", import.meta.url).href
const nothingData = new URL("@/assets/porduct/icon_zanwushuju.png", import.meta.url).href
// const lsxsjImage = new URL("@/assets/home/icon_lansexiasanjiao.png", import.meta.url).href;

const route = useRoute();
const router = useRouter();
/** 开始时间 */
const startTime: any = computed(() => {
    return route.query.startTime;
});
/** 结束时间 */
const endTime: any = computed(() => {
    return route.query.endTime;
});

const switchList = ref(['待处理', '已处理'])
const switchIndex = ref(0)
const categoryShow = ref(false)
const categoryName = ref('选品部门')
// 选品部门
const categoryList = ref<any>([])
// 限制连续刷新数据重复问题
const refreFlag = ref(true)
const refreshing = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = ref<any[]>([]);

const showPicker = ref(false);
const currentDate = ref([startTime.value])
// const currentDate = ref([startTime.value.split('-')[0], startTime.value.split('-')[1], startTime.value.split('-')[2]])
const currentDateBottom = ref([endTime.value])
// const currentDateBottom = ref([endTime.value.split('-')[0], endTime.value.split('-')[1], endTime.value.split('-')[2]])
// 限制最大可选时间
const maxDate = computed(() => {
    return new Date(currentDateBottom.value.join('/'))
})

// 限制最小可选时间
const minDate = computed(() => {
    return new Date(currentDate.value.join('/'))
})
const showPickerBottom = ref(false)
const currentDateFormatInterval = ref<any>(endTime.value)

const currentDateFormat = ref<any>(currentDate.value.join("-"))
const queryParams = ref<any>({
    pageNum: 1,
    pageSize: 10,
    coreId: '',
    status: 0
})
/** 日期选择 */
const onConfirm = () => {
    showPicker.value = false;
    currentDateFormat.value = currentDate.value.join("-")
    onRefresh()
};
/** 日期选择 */
const onConfirmBottom = () => {
    showPickerBottom.value = false
    currentDateFormatInterval.value = currentDateBottom.value.join("-")
    onRefresh()
}

/** 获取选品主播 */
const getAnchor = async () => {
    const res = await queryOperationDeptList();
    categoryList.value = res.data.map(item => {
        return {
            name: item.deptName,
            status: item.deptId,
            color: '#1A1B1C'
        }
    })
    categoryList.value.unshift({
        name: '全部',
        status: null,
        remark: '全部',
        deptId: null,
        color: '#0094FF'
    })
}
getAnchor()
/** 选品部门选择事件 */
const onSelectCategory = (selection) => {
    categoryShow.value = false;
    categoryName.value = selection.name == '全部' ? '选品部门' : selection.name
    categoryList.value.forEach((item: any) => { item.color = '#1A1B1C' })
    selection.color = '#0094FF'
    queryParams.value.coreId = selection.status
    onRefresh()
}
/** 跳转详情 */
const handleGetDetails = (id: string, item: any) => {
    router.push({
        path: '/selectProductsDetail',
        query: {
            id,
            datas: JSON.stringify(item),
            switchIndex: switchIndex.value,
            type: route.query.type
        }
    })
}
/** 回退 */
const handleLeft = () => {
    if (route.query.from == 'research') {
        router.push('/research/researchPage')
    } else {
        router.push('/home/index')
    }
}
/** 切换点击 */
const switchClick = (index: number) => {
    switchIndex.value = index
    queryParams.value.status = index
    onRefresh()
}
/** 查询商品列表 */
const getList = async () => {
    loading.value = true
    var res = await getSelectedProduct({
        ...queryParams.value,
        selectionStartTime: currentDateFormat.value,
        selectionEndTime: currentDateFormatInterval.value,
        type: route.query.type
    });
    setTimeout(() => {
        refreFlag.value = true
        // 空数据 停止加载
        if (res?.rows?.length == 0) {
            list.value = [];
            finished.value = true;
            loading.value = false;
            return;
        }
        // 有数据 赋值列表 加载状态结束
        res?.rows?.forEach((item) => {
            finished.value = false;
            list.value.push(item);
        });
        loading.value = false;
        refreshing.value = false;
        // 如果list长度大于等于总数据条数，数据全部加载完成
        if (list.value.length >= res.total) {
            finished.value = true; //结束加载
        }
    }, 400);
};
/** 加载更多 */
const onLoad = async () => {
    getList();
    queryParams.value.pageNum++;
};
/** 下拉 */
const onRefresh = () => {
    if (refreFlag.value) {
        refreFlag.value = false
        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        queryParams.value.pageNum = 1; // 分页数赋值为1
        list.value = []; // 清空数组
        onLoad(); // 重新加载数据
    } else {
        finished.value = false;
        refreshing.value = false;
    }
};
</script>
<style lang="less" scoped>
* {
    box-sizing: border-box;
}

.selectProducts {
    height: 100vh;
    width: 100%;
    background: #F6FAFF;
    box-sizing: border-box;
    overflow: hidden;

    .selectProducts_back {
        width: 375px;
        height: 133px;
        background: linear-gradient(#CDECFF 0%, #ffffff 100%);
        filter: blur(0px);

        .bg-white-height {
            height: 44px;
            width: 100%;
        }

        .van-nav-bar {
            height: 46px;
            width: 100%;
            background-color: inherit;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;

            .title {
                font-size: 4.266667vw;
                color: #1a1b1c;
            }

            .left_img {
                position: absolute;
                left: 15px;
                color: #000;
            }
        }

        .switch {
            height: 43px;
            width: 100%;
            background-color: inherit;
            display: flex;
            align-items: center;
            justify-content: center;

            .switch_div {
                width: 208px;
                height: 35px;
                background: #E1EEFF;
                border-radius: 18px;
                display: flex;
                align-items: center;
                justify-content: space-between;

                .switch_div_item {
                    width: 50%;
                    height: 100%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-size: 14px;
                    color: #1A1B1C;
                }

                .switch_div_item_active {
                    width: 50%;
                    height: 100%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-size: 14px;
                    background: linear-gradient(270deg, #0064FF 0%, #008FFF 100%);
                    border-radius: 16px;
                    color: #FFFFFF;
                }
            }

        }


    }

    .approval_screen {
        width: 375px;
        height: 44px;
        background: #FFFFFF;
        margin: 10px 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 14px;
        padding: 0 15px;
        box-sizing: border-box;

        :deep(.van-image) {
            width: 10px;
            height: 6px;
            margin-left: 4px;
            position: relative;
        }

        .approval_screen_category {
            display: flex;
            align-items: center;
        }

        .home-index-data-date {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: #0094ff;
            width: calc(100% - 115px);
            justify-content: flex-end;

            &-first {
                display: flex;
                align-items: center;

                &-date {
                    margin-right: 5px;
                }
            }

            &-middle {
                margin: 0 10px;
            }

            &-second {
                display: flex;
                align-items: center;

                &-date {
                    margin-right: 5px;
                }
            }
        }

        .approval_screen_date {
            display: flex;
            align-items: center;
        }
    }

    .van-pull-refresh {
        height: calc(100vh - 197px);
        overflow: auto;
    }

    .nothing_div {
        width: 100%;
        margin: auto;
        text-align: center;
        margin-top: 20vh;
        font-weight: 400;
        font-size: 12px;
        color: #B8BECA;
        line-height: 12px;
        flex-wrap: wrap;

        .van-image {
            width: 160px;
            height: 160px;
        }
    }

    .product_list_van_list {
        padding: 0 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        background: #f6faff;

        .product_list_van_list_div {
            width: 100%;
            height: 104px;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 3px;
            margin-bottom: 8px;
            padding: 5px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            padding: 10px 15px 10px 15px;
            display: flex;
            align-items: center;
            align-content: space-between;

            .titles {
                width: 100%;
                font-weight: 500;
                font-size: 16px;
                color: #1A1B1C;
            }

            .flex {
                display: flex;
                align-items: center;
                justify-content: space-between;
            }

            .center {
                width: 100%;

                .title {
                    font-weight: 400;
                    font-size: 14px;
                    color: #1A1B1C;
                }

                .number {
                    font-weight: 600;
                    font-size: 14px;
                    color: #0094FF;
                }
            }

            .bottom {
                width: 100%;

                .grey {
                    font-weight: 400;
                    font-size: 14px;
                    color: #8492AD;
                }
            }
        }
    }

}
</style>